﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using HTML;
using Graph;


namespace GUI
{
    public partial class Form1 : Form
    {
        TreeNode node1;
        BasicGraphImpl baseGraph;
        int breakMe;
        
        public Form1()
        {
            InitializeComponent();
            baseGraph = new BasicGraphImpl();
        }


        private void GoButton_Click(object sender, EventArgs e)
        {
            //WebClient client = new WebClient();
            //try
            //{

            //    webPageTextbox.Text = client.DownloadString(urlTextBox.Text);
            //    button1.Enabled = true;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("Ikke valid hyperlink","Fejl");
            //}
            LinksBox.Items.Clear();
            HtmlDoc test = new HtmlDoc(urlTextBox.Text);
            foreach (Vertex v in BreadthFirst(test.GetAllLinks()))
            {
                if (v.getName().Contains(".ico") || v.getName().Contains(".css") || v.getName().Contains("  "))
                {

                }
                else
                {
                    LinksBox.Items.Add(v.getName());
                }
                
            }
            linksCounter.Text = LinksBox.Items.Count.ToString();
        }

        //public IList<Vertex> BreadthFirst(IList<string> links)
        //{
        //    Vertex startVer = new Vertex(urlTextBox.Text);
        //    foreach (var l in links)
        //    {
        //        startVer.AddEdge(new Vertex(l));
        //    }
        //    foreach (Vertex v in baseGraph.GetAdjacencies(startVer))
        //    {
        //        HtmlDoc level1 = new HtmlDoc(v.getName());
        //        //startVer.AddEdge(new Vertex(v.getName()));
        //        foreach (Vertex v2 in baseGraph.GetAdjacencies(v))
        //        {
        //            HtmlDoc level2 = new HtmlDoc(v2.getName());
        //            v.AddEdge(new Vertex(v2.getName()));
        //            foreach (Vertex v3 in baseGraph.GetAdjacencies(v2))
        //            {
        //                HtmlDoc level3 = new HtmlDoc(v3.getName());
        //                v2.AddEdge(new Vertex(v3.getName()));
        //                foreach (Vertex v4 in baseGraph.GetAdjacencies(v3))
        //                {
        //                    HtmlDoc level4 = new HtmlDoc(v4.getName());
        //                    v3.AddEdge(new Vertex(v4.getName()));
        //                }
        //            }
        //        }
        //    }
        //    return baseGraph.GetAllVertexesByBreadthFirst(startVer);
        //}

        public IList<Vertex> BreadthFirst(IList<string> links)
        {
            Vertex startVer = new Vertex(urlTextBox.Text);
            BreadthFirst2(links, startVer);
            return baseGraph.GetAllVertexesByBreadthFirst(startVer);
        }

        public void BreadthFirst3(IList<string> links, Vertex ver)
        {
            try
            {
                foreach (var k in links)
                {
                    ver.AddEdge(new Vertex(k));
                }
                foreach (Vertex v in baseGraph.GetAdjacencies(ver))
                {
                    HtmlDoc level2 = new HtmlDoc(v.getName());

                    BreadthFirst4(level2.GetAllLinks(), v);
                    //startVer.AddEdge(new Vertex(v.getName()));
                }
                
            }
            catch (Exception)
            {
                 
            }
        }
        public void BreadthFirst4(IList<string> links, Vertex v)
        {
            foreach (var k in links)
            {
                v.AddEdge(new Vertex(k));
            }
            foreach (Vertex ver in baseGraph.GetAdjacencies(v))
            {
                HtmlDoc level2 = new HtmlDoc(ver.getName());
                //startVer.AddEdge(new Vertex(v.getName()));
            }
        }
        

        public void BreadthFirst2(IList<string> links, Vertex ver)
        {
            
            foreach (var l in links)
            {
                ver.AddEdge(new Vertex(l));
            }
            foreach (Vertex v in baseGraph.GetAdjacencies(ver))
            {
                node1 = new TreeNode(v.getName());
                treeView.Nodes.Add(node1);
                HtmlDoc level1 = new HtmlDoc(v.getName());

                BreadthFirst3(level1.GetAllLinks(), v);
                //startVer.AddEdge(new Vertex(v.getName()));
            }
        }

        private void addEdges(IList<String> link, Vertex v)
        {
            foreach (var l in link)
            {
                v.AddEdge(new Vertex(l));
            }
        }

      
       
        private void webPageTextbox_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }
    }
}
